<?php
namespace App\Controller\App;

use App\Query\UserTransferQuery;
use App\Repository\UserRepository;
use App\Repository\UserTransferRepository;

use App\Service\Code\CodeService;

class UserTransferController extends Controller
{
    // 日志
    public function index()
    {
        $query = $this->request->input('type') ? UserTransferQuery::toUser($this->auth()) : UserTransferQuery::user($this->auth());
        
        return $query->paginate(15)->toArray();
    }
    
    // 优惠转帐
    public function discount($id)
    {
        $user = $this->auth();
        $level = $this->level();
        
        // 取出用户
        if(!$toUser = UserRepository::find($id))
        {
            $this->error('user');
        }
        
        if(!(new CodeService($user->phone))->check($this->input('code')))
        {
            $this->error('code');
        }

        // 转帐金额
        $money = $this->input('money');
        
        // 是否开启
        if(!$level->discountTransfer())
        {
            $this->error('on');
        }
        
        // 税
        $tax = $level->discountTransferMoney($money);
        
        // 扣优惠
        if(!$user->moneyDiscountMake($money, '转帐给【' . $toUser->username . '】'))
        {
            $this->error('money_discount');
        }

        // 加优惠
        $toUser->moneyDiscountAdd($money, '来自【' . $user->username . '】转帐');
        
        // 扣税
        $toUser->moneyDiscountMake($tax, '来自【' . $user->username . '】转帐扣税');
        
        // 记录
        UserTransferRepository::make()->create($user, $toUser, [
            'money_type' => 'money_discount',
            'money'      => $money,
            'tax'        => $tax
        ]);
        
        $this->message('ok');
    }
    
    // 优惠转帐
    public function full($id)
    {
        $user = $this->auth();
        $level = $this->level();
        
        // 取出用户
        if(!$toUser = UserRepository::find($id))
        {
            $this->error('user');
        }
        
        if(!(new CodeService($user->phone))->check($this->input('code')))
        {
            $this->error('code');
        }

        // 转帐金额
        $money = $this->input('money');
        
        // 是否开启
        if(!$level->fullTransfer())
        {
            $this->error('on');
        }
        
        // 税
        $tax = $level->fullTransferMoney($money);
        
        // 扣优惠
        if(!$user->moneyFullMake($money, '转帐给【' . $toUser->username . '】'))
        {
            $this->error('money_full');
        }

        // 加优惠
        $toUser->moneyFullAdd($money, '来自【' . $user->username . '】转帐');
        
        // 扣税
        $toUser->moneyFullMake($tax, '来自【' . $user->username . '】转帐扣税');
        
        // 记录
        UserTransferRepository::make()->create($user, $toUser, [
            'money_type' => 'money_full',
            'money'      => $money,
            'tax'        => $tax
        ]);
        
        $this->message('ok');
    }
    
    // 补贴转优惠
    public function fullDiscount()
    {
        $user = $this->auth();
        $level = $this->level();
        
        // 金额
        $money = $this->input('money');

        // 是否开启
        if(!$level->fullDiscount())
        {
            $this->error('on');
        }

        // 扣补贴
        if(!$user->moneyFullMake($money, '补贴转换优惠'))
        {
            $this->error('money_full');
        }

        // 加优惠
        $user->moneyDiscountAdd($money, '补贴转换优惠');

        // 扣税
        $user->moneyDiscountMake($level->fullDiscountMoney($money), '补贴转换优惠扣税');
        
        $this->message('ok');
    }
}
